package com.yaojiaxun.logisticsPlatformSystem.service;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yaojiaxun.logisticsPlatformSystem.dto.CompanyInfoDTO;
import com.yaojiaxun.logisticsPlatformSystem.dto.PageDTO;
import com.yaojiaxun.logisticsPlatformSystem.entity.CompanyInfo;
import com.yaojiaxun.logisticsPlatformSystem.entity.CompanySearch;
import com.yaojiaxun.logisticsPlatformSystem.mapper.CompanyInfoMapper;
import com.yaojiaxun.logisticsPlatformSystem.util.SnowFlakeIDWorker;
import com.yaojiaxun.logisticsPlatformSystem.util.TimeFormat;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Auther: yjx_j
 * @Date: 2019/1/18 17:16
 * @Description:
 */
@Service
public class CompanyInfoService {

    @Autowired
    CompanyInfoMapper companyInfoMapper;

    public void addCompanyInfo(CompanyInfo companyInfo){
        companyInfo.setCompanyId(new SnowFlakeIDWorker(2,1).nextId());
        companyInfo.setCreateTime(Instant.now().toEpochMilli());
        companyInfo.setUpdateTime(Instant.now().toEpochMilli());
        companyInfoMapper.addCom(companyInfo);
    }

    public CompanyInfoDTO beanToDTO(CompanyInfo companyInfo){
        if (companyInfo == null)
            return null;
        CompanyInfoDTO companyInfoDTO = new CompanyInfoDTO();
        companyInfoDTO.setCreateTime(TimeFormat.convertTime(companyInfo.getCreateTime()));
        companyInfoDTO.setUpdateTime(TimeFormat.convertTime(companyInfo.getUpdateTime()));
        BeanUtils.copyProperties(companyInfo, companyInfoDTO);
        return companyInfoDTO;
    }

    public PageDTO<CompanyInfoDTO> findPage(Integer pageNum){
        PageDTO<CompanyInfoDTO> pageDTO = new PageDTO<>();
        Page<CompanyInfoDTO> page = new Page<>(pageNum, 10);
        List<CompanyInfoDTO> results = companyInfoMapper.selectPage(page, null).stream().map(this::beanToDTO).collect(Collectors.toList());
        pageDTO.setPage(page);
        pageDTO.setResult(results);
        return pageDTO;
    }

    public CompanyInfo findCompanyById(Long companyId){
        return companyInfoMapper.findCompanyById(companyId);
    }

    public Boolean updateCompanyInfo(CompanyInfo companyInfo){
        return companyInfoMapper.updateComInfo(companyInfo)>0;
    }

    public void deleteCompany(Long companyId){
        companyInfoMapper.deleteCompany(companyId);
    }

    public CompanyInfo findCompanyByName(String companyName){
        return companyInfoMapper.findCompanyByName(companyName);
    }

    //取出后6条数据
    public List<CompanyInfoDTO> findLast6Company(){
        List<CompanyInfo> companies = companyInfoMapper.findLast6Company();
        List<CompanyInfoDTO> list = new ArrayList<>();
        for (int i =0; i<companies.size(); i++){
            CompanyInfoDTO companyInfoDTO = beanToDTO(companies.get(i));
            companyInfoDTO.setUpdateTime(TimeFormat.convertTime(companies.get(i).getUpdateTime()));
            list.add(companyInfoDTO);
        }
        return list;
    }

    //公司名模糊搜索
    public Wrapper<CompanyInfo> getWrapper(CompanySearch companySearch){
        if (companySearch == null)
            return null;
        Wrapper<CompanyInfo> wrapper = new EntityWrapper<>();
        wrapper.like("company_name", companySearch.getCompanyName(), SqlLike.DEFAULT);
        return wrapper;
    }

    public PageDTO<CompanyInfoDTO> findPage(Integer pageNum, CompanySearch companySearch){
        PageDTO<CompanyInfoDTO> pageDTO = new PageDTO<>();
        Page<CompanyInfoDTO> page = new Page<>(pageNum, 10);
        List<CompanyInfoDTO> result = companyInfoMapper.selectPage(page, getWrapper(companySearch)).stream().map(this::beanToDTO).collect(Collectors.toList());
        pageDTO.setPage(page);
        pageDTO.setResult(result);
        return pageDTO;
    }
}
